// ignore_for_file: prefer_typing_uninitialized_variables, library_private_types_in_public_api

import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class ShowInputAlertWidget extends StatefulWidget {
  final confirmCallback;
  final title;
  final placeholder;
  final keyBoardType;
  const ShowInputAlertWidget(
      this.confirmCallback, this.title, this.placeholder, this.keyBoardType,
      {super.key});

  @override
  _ShowInputAlertWidgetState createState() => _ShowInputAlertWidgetState();
}

class _ShowInputAlertWidgetState extends State<ShowInputAlertWidget> {
  String inputValue = '';

  @override
  Widget build(BuildContext context) {
    return CupertinoAlertDialog(
      title: Text(widget.title),
      content: Column(
        children: <Widget>[
          const SizedBox(
            height: 10,
          ),
          CupertinoTextField(
            keyboardType: widget.keyBoardType,
            placeholder: widget.placeholder,
            onChanged: (value) {
              inputValue = value;
            },
          )
        ],
      ),
      actions: <Widget>[
        CupertinoDialogAction(
          child: const Text("取消"),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        CupertinoDialogAction(
          child: const Text("确定"),
          onPressed: () {
            if (widget.title == '心率') {
              int heartValue = int.parse(inputValue);
              if (heartValue < 0 || heartValue > 200) {
                EasyLoading.showToast('请输入0～200之间的心率');
                return;
              }
            }
            if (widget.title == '睡眠') {
              int heartValue = int.parse(inputValue);
              if (heartValue < 1 || heartValue > 10) {
                EasyLoading.showToast('请输入1～10之间的睡眠小时');
                return;
              }
            }
            widget.confirmCallback(inputValue);
            Navigator.pop(context);
          },
        ),
      ],
    );
  }
}
